import config from "../config";
import axios from "axios";

const apiKey = config.deepL.apiKey;

// 创建axios实例
const request = axios.create({
  baseURL: "/deepL",
  timeout: 100000,
});

// 请求拦截器
request.interceptors.request.use(
  (config) => {
    config.headers = {
      Authorization: `DeepL-Auth-Key ${apiKey}`,
      "Content-Type": "application/json",
    };
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
request.interceptors.response.use(
  (response) => {
    if (response.status === 200) {
      return response.data.translations[0];
    } else {
      return Promise.reject(response);
    }
  },
  (error) => {
    return Promise.reject(error);
  }
);

export const deepLLP = (text, targetLang = "ZH") => {
  return request.post("/v2/translate", {
    tag_handling: "html", // 启用HTML处理
    ignore_tags: ["code", "pre"], // 可选：不翻译这些标签内的内容
    outline_detection: false, // 复杂HTML建议关闭大纲检测
    text: [text],
    target_lang: targetLang,
  });
};
